package selcoursesys.dao.impl;

import org.hibernate.*;
import selcoursesys.dao.UserDAO;
import selcoursesys.entity.UserEntity;
import db.MyHibernateSessionFactory;

import java.util.Iterator;
import java.util.List;

public class UserDAOImpl implements UserDAO {
    private Session session;

    public List<UserEntity> checkUser(UserEntity user){
       session=MyHibernateSessionFactory.getSession();
       String hql="From UserEntity usee where usee.upwd=:upwd";
       Query query=session.createQuery(hql);
       query.setString("upwd",user.getUpwd());
       List list=query.list();
       if(list.size()>0){
           return list;
       }else{
           return null;
       }
    }
    /**
     * hql批量删除
     *
     */
    public boolean delUser(UserEntity user){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            String hql="delete UserEntity where upwd='000000'";
            Query query=session.createQuery(hql);
            query.executeUpdate();
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            session.close();
            return true;
        }
    }
    /**
     * hql批量插入
     */
    public boolean insertUser(UserEntity user){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            for(int i=0;i<10;i++){
                UserEntity usertest=new UserEntity();
                usertest.setUid("u"+i);
                usertest.setUpwd("123456");
                usertest.setUtype("stu");
                session.save(usertest);

                if (i%5==0){
                    session.flush();
                    session.clear();
                }
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            session.close();
            return true;
        }
    }
    /**
     * hql更新数据
     *
     */
    public boolean updateUser(UserEntity user){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            String hql="update UserEntity usee set upwd='111111' where usee.utype=:stu";
            Query query=session.createQuery(hql);
            query.setString("stu","stu");
            query.executeUpdate();
            tx.commit();
        }catch(Exception e){
            e.printStackTrace();
            tx.rollback();
        }finally{
            session.close();
        }
        return false;
    }
    public List<UserEntity> queryUser(UserEntity user){
        //开启session
        session=MyHibernateSessionFactory.getSession();
        /**
         * hql 条件查询
         * String hql="From UserEntity usee where usee.utype=:tea";
         * Query query=session.createQuery(hql);
         * query.setString("tea","tea");
         *
         * */
        String hql="From UserEntity usee where usee.utype=:tea";
        Query query=session.createQuery(hql);
        query.setString("tea","tea");
        List list=query.list();
        Iterator itor=list.iterator();
        while(itor.hasNext()){
            UserEntity usertest=(UserEntity)itor.next();
            System.out.print(usertest.getUid()+" "+usertest.getUpwd()+" "+usertest.getUtype()+"\n");
        }
        return list;
    }

    @Override
    public UserEntity userLogin(UserEntity userEntity) {
        session=MyHibernateSessionFactory.getSession();
        String hql="From UserEntity user where user.uid=:uid";
        Query query=session.createQuery(hql);
        query.setString("uid",userEntity.getUid());
        List list=query.list();
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            UserEntity userEntity1=(UserEntity)iterator.next();
            return userEntity1;
        }
        return null;
    }
}
